﻿@{
    ViewBag.Title = "盘库页面";
    Layout = "~/Areas/IM/Views/Shared/_Layout.cshtml";
}
@section Scripts{
    <script type="text/javascript">
        var Materials;
        var boolSearch = true;
        $(document).ready(function () {
            DisableBackspace();
            bindGrid();
            initSearch();
            Materials = new Array();
        });
        $(window).resize(function () {
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridSurplusList");
        });
        function bindGrid() {
            $("#gridSurplusList").datagrid({
                type: "POST",
                nowrap: false,
                width: 'auto',
                height: 'auto',
                fitColumns: true,
                striped: true,
                singleSelect: true,
                url: '@Url.Content("~/IM/I_Surplus/GetSurplusGroupList")',
                loadMsg: '数据加载中请稍后……',
                pagination: true,//启用分页，默认每页10行
                rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
                pageSize: 15,//设置 页容量5页
                pageList: [10, 15, 20],//设置 页容量下拉框
                queryParams: {
                    SurplusCode: $("#Storage").combobox("getValue"),
                    MaterialType: $("#MaterialTypeInfo").val(),
                    MaterialID: $("#MaterialID").val()
                },
                columns: [
                    [
                    { field: "StorageName", title: "所属仓库", width: '15%', align: "center" },
                    { field: "MaterialName", title: "物资名称", width: '20%', align: "center" },
                    { field: "Surplus", title: "库存量", width: '10%', align: "right" },                    
                    { field: "RealBatchNo", title: "批次号", width: '10%', align: "right" }, //2018-12-11 zch
                    { field: "MaterialSpecification", title: "规格", width: '8%', align: "center" },
                    { field: "MaterialUnitName", title: "单位", width: '8%', align: "center" },
                    { field: "MaterialManufacturer", title: "生产厂家", width: '15%', align: "center" },
                    { field: "MaterialVendor", title: "供应商", width: '10%', align: "center" },
                    { title: 'ID', field: 'ID', sortable: false, hidden: true },
                    { title: 'StorageCode', field: 'StorageCode', sortable: false, hidden: true },
                    { title: 'MaterialID', field: 'MaterialID', sortable: false, hidden: true },
                    { title: 'MaterialMTypeID', field: 'MaterialMTypeID', sortable: false, hidden: true },
                    { title: 'MaterialAlarmCounts', field: 'MaterialAlarmCounts', sortable: false, hidden: true }
                    ]
                ],
                view: detailview,
                detailFormatter: function (index, row) {
                    return '<div style="width:100%"><table style="padding:2px" id="ddv' + index + '"></table></div>';
                },
                onExpandRow: function (index, row) {
                    $('#ddv' + index).datagrid({
                        type: "POST",
                        url: '@Url.Content("~/IM/I_Surplus/GetMaterialSurplusList")',
                        queryParams: {
                            SurplusCode: row.StorageCode,
                            MaterialID: row.MaterialID
                        },
                        fitColumns: true,
                        singleSelect: true,
                        rownumbers: true,
                        nowrap: false,
                        striped: true,//是否显示斑马线效果
                        height: 'auto',
                        width: '95%',
                        pagination: true,//启用分页，默认每页10行
                        rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
                        pageSize: 10,//设置 页容量5页
                        pageList: [10, 20, 40],//设置 页容量下拉框
                        frozenColumns: [
                            [
                            { field: "MaterialName", title: "物资名称", width: '20%', align: "center" },
                            { field: "RealBatchNo", title: "批次号", width: '15%', align: "center", formatter: $.formatRealBatchNo },
                            { field: "ValidityDate", title: "有效期", width: '12%', align: "center", sortable: true, formatter: $.formatValidityDate },
                            { field: "Surplus", title: "库存量", width: '12%', align: "center" },
                            { field: "MaterialUnitName", title: "单位", width: '8%', align: "center" },
                            { field: "MaterialSpecification", title: "规格", width: '8%', align: "center" },
                            {
                                field: 'opt', title: '盘点', width: '10%', align: 'center', frozen: 'true',
                                formatter: function (value, rec, i) {
                                    return '<a  class="pandiancls" onclick="openPanKu(\'#ddv' + index + '\',' + i + ');"  href="javascript:void(0)">盘点库存</a>';
                                }
                            },
                            { title: 'ID', field: 'ID', hidden: true },
                            { title: 'StorageCode', field: 'StorageCode', hidden: true },
                            { title: 'MaterialID', field: 'MaterialID', hidden: true },
                            { title: 'BatchNo', field: 'BatchNo', hidden: true },
                            { title: 'MaterialMTypeID', field: 'MaterialMTypeID', hidden: true },
                            { title: 'MaterialAlarmCounts', field: 'MaterialAlarmCounts', hidden: true },
                            { field: "MaterialManufacturer", title: "MaterialManufacturer", hidden: true },
                            { field: "MaterialVendor", title: "MaterialVendor", hidden: true },
                            ]
                        ],
                        onResize: function () {
                            $('#gridSurplusList').datagrid('fixDetailRowHeight', index);
                        },
                        onLoadSuccess: function (data) {
                            setTimeout(function () {
                                $('#gridSurplusList').datagrid('fixDetailRowHeight', index);
                            }, 0);
                            $('.pandiancls').linkbutton({
                                text: '盘点库存',
                                plain: true,
                                iconCls: 'icon-custom-cPanKu',
                            });
                        }

                    });
                    $('#gridSurplusList').datagrid('fixDetailRowHeight', index);
                },
                rowStyler: function (index, row) {
                    if (row.Surplus < row.MaterialAlarmCounts) {
                        return 'color:red';
                    }
                }
            });
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridSurplusList");
        }
        function reSearch() {
            $("#gridSurplusList").datagrid({
                pageNumber: 1,//20151105增加点击查询按钮返回第一页
                queryParams: {
                    SurplusCode: $("#Storage").combotree("getValue"),
                    MaterialType: $("#MaterialTypeInfo").val(),
                    MaterialID: $('#Material').combogrid('getValue')

                    //MaterialID: $("#MaterialID").val()
                }
            });
        }
        function openPanKu(gid, index) {
            ////隐藏数据
            $(gid).datagrid('selectRow', index);// 关键在这里
            var selected = $(gid).datagrid('getSelected');
            $('#RealBatchNo').val(selected.RealBatchNo);
            $('#BatchNo').val(selected.BatchNo);
            $('#MaterialID').val(selected.MaterialID);
            $('#DeliveryCounts').val(selected.Surplus);
            $('#StorageCode').val(selected.StorageCode);
            //显示数据
            $('#SurplusRealBatchNo').textbox('setText', $.formatRealBatchNo(selected.RealBatchNo));
            $('#SurplusMaterialName').textbox('setText', selected.MaterialName);
            $('#SurplusMaterialManufacturer').textbox('setText', selected.MaterialManufacturer);
            $('#SurplusMaterialVendor').textbox('setText', selected.MaterialVendor);
            $('#SurplusValidityDate').textbox('setText', $.formatValidityDate(selected.ValidityDate));
            $('#SurplusStorage').textbox('setText', selected.StorageName);
            $('#editEntryCounts').textbox('setText', selected.Surplus);
            initpage(selected.Surplus);
            $('#StockSurplusInfo').dialog('open');
        }
        function initSearch() {
            var urlstr1 = '@Url.Content("~/IM/AllDictionary/GetStorageCombo")';
            $.EUIcombobox("#Storage", {
                url: urlstr1,
                valueField: 'ID',
                textField: 'Name',
                editable: false,
                panelHeight: 'auto',
                datainfo: {},
                OneOption: [{
                    ID: "",
                    Name: "--请选择--"
                }]
            });
            var urlstr2 = '@Url.Content("~/IM/AllDictionary/GetMaterialTypeByUserInfo")';
            $.EUIcomboboxTree("#MaterialType", {
                url: urlstr2,//ajax后台取数据路径，返回的是json格式的数据
                OneOption: [{
                    id: "",
                    text: "--请选择--",
                }],
                datainfo: {},
                onSelect: function (rec) {
                    if (rec.id == "") {
                        $("#MaterialID").val("");
                        $("#MaterialTypeInfo").val("");
                        return;
                    }
                    var urlstr3 = '@Url.Content("~/IM/AllDictionary/GetMaterial?mType=")' + rec.id;
                    $("#MaterialTypeInfo").val(rec.id);
                    $("#Material").combogrid({
                        prompt: '--请选择--',
                        panelWidth: 320,
                        idField: 'ID',
                        textField: 'Name',
                        groupField: 'QRCode',
                        columns: [[
                            { field: 'MCode', title: '代码', width: 50, align: 'right' },
                            { field: 'Name', title: '名称', width: 100 },
                            { field: 'UnitName', title: '单位', width: 50 },
                            { field: 'Specification', title: '规格', width: 50, align: 'right' },
                            { field: 'RealPrice', title: '售价', width: 50 },
                            { field: 'Unit', title: '单位', width: 50, sortable: false, hidden: true }
                        ]],
                        fitColumns: true,
                        filter: function (inputText, row) {
                            var opts = $(this).combogrid('options');
                            if (row[opts.groupField] == inputText) {
                                return true;
                            } else if (row[opts.textField] == inputText) {
                                return true;
                            } else {
                                if (boolSearch) {
                                    boolSearch = false;
                                    searchBindMeterialbyName(urlstr3, inputText);
                                }
                            }
                        },
                        onChange: function (n, o) {
                            if (n == "") {
                                searchBindMeterialbyName(urlstr3, n);
                                $("#MaterialID").val("");
                            }
                        },
                        onSelect: function (rec) {
                            var g = $("#Material").combogrid("grid");	// 获取数据表格对象
                            var r = g.datagrid('getSelected');	// 获取选择的行
                            $("#MaterialID").val($('#Material').combobox('getValue'));
                        }
                    });
                    searchBindMeterialbyName(urlstr3, "");
                }
            });
        }
        function searchBindMeterialbyName(urinfo, keyWord) {
            $.ajax({
                type: 'POST',
                url: urinfo,
                dataType: "json",
                success: function (jsData) {
                    if (keyWord == "") {
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    } else {
                        for (var i = 0; i < jsData.length; i++) {
                            if (jsData[i].Name.indexOf(keyWord) < 0) {
                                if (jsData[i].MCode.indexOf(keyWord) < 0) {
                                    if (jsData[i].PinYin.indexOf(keyWord) < 0) {
                                        jsData.remove(jsData[i]);
                                        i--;
                                    }
                                }
                            }
                        }
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    }
                    boolSearch = true;
                }
            });
        }

        //2018-12-11 zch
        function openPrintURL() {
            var SurplusCode = $("#Storage").combobox("getValue");
            var MaterialType = $("#MaterialTypeInfo").val();
            var MaterialID = $("#MaterialID").val();

            var url = '@Url.Content("~/BB/LS/LS_SurplusListPrint/?SurplusCode=")' + SurplusCode + '&MaterialType=' + MaterialType + '&MaterialID=' + MaterialID;
            window.open(url);
        }

    </script>
}
<div id="TableForm" class="easyui-layout" style="width:auto; height: auto; margin-left: auto; margin-right: auto">
    <div data-options="region:'center',title:'物资清单',iconCls:'icon-custom-cPanKu'">
        <div id="SearchBar" data-options="region:'north'" style="overflow: hidden; padding: 10px; position: relative;">
            <table>
                <tr>
                    <td>仓库：</td>
                    <td>
                        <input id="Storage" class="easyui-combotree" data-options="required:true" style="width:160px" />
                    </td>
                    <td>分类：</td>
                    <td>
                        <input id="MaterialType" class="easyui-combobox" style="width:160px" />
                        <input id="MaterialTypeInfo" type="hidden" />
                    </td>
                    <td>物资：</td>
                    <td>
                        <input id="Material" class="easyui-combogrid" style="width:160px" />
                        <input id="MaterialID" type="hidden" />
                    </td>
                    <td>
                        &nbsp;&nbsp;
                        <a href="javascript:void(0)" id="btnsearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="reSearch()">查询</a>
                    </td>
                    <td align="right">

                        <a href="javascript:void(0)" id="btnsearch" class="easyui-linkbutton" data-options="iconCls:'icon-print'" onclick="openPrintURL()">打印</a>
                        &nbsp;&nbsp;
                    </td>
                </tr>
            </table>
            <div style="position:absolute;right:10px; bottom:2px;">注：<span style="color:red">该物资数量低于预警数量</span></div>
        </div>
        <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
            <table id="gridSurplusList"></table>
        </div>
    </div>
</div>
@Html.Partial("~/Areas/IM/Views/I_Surplus/_StockSurplus.cshtml")
